#此扩展用于的模块扩展处理,用于支撑模块的
#   SUPPORT_3RD_PKG_NAME:
#       用于标准后期适配库的包名称
#       EWX里后期脚本使用变量都与此变量有关系
#       查找包的名称可与之相同也可以不同
#   EWX_HAS_CONAN_ENV:
#       此变量是公共cmake模块查找conan环境决定的变量类型
#       目的是为了支撑conan方式和非conan方式的同时处理
#   此模块主要决定以下几个环境支撑
#   [1]查找模块
#   [2]编绎目标
#   [3]调试环境变量:
#       生成VS调试环境的变量处理
#   安装:
#       自动安装库文件到安装目录
include(support_3rd_utils)

SET(SUPPORT_3RD_PKG_NAME osgEarth)

IF(EWX_HAS_CONAN_ENV)
    message(NOTICE "${SUPPORT_3RD_PKG_NAME} Use Conan Env Mode....")
    find_package(osgearth REQUIRED)
    set(osgearth_LIBRARY
        osgearth::osgEarth
        osgearth::osgEarthUtil 
        osgearth::osgEarthFeatures
        osgearth::osgEarthSymbology
    )
    set(osgEarth_LIBRARY
        ${osgearth_LIBRARY}
    )

    #调试环境变量
    MAKE_3RD_PKG_RUNTIME(${SUPPORT_3RD_PKG_NAME} ${CONAN_OSGEARTH_ROOT})

	IF(EXISTS ${EWX_SDK_ROOT})
		return()
	ENDIF()
    #打包使用
    if(WIN32)
		#========bin======
		install(CODE "MESSAGE(\"install ${PKG_NAME} dlls...\")")
    	file(GLOB ALL_DLLS ${CONAN_OSGEARTH_ROOT}/bin/*.dll)
		COMMON_GET_RELEASE_RUNTIME_FILES(ALL_DLLS REL_DLLS)
      	install(FILES ${REL_DLLS} 
			DESTINATION bin 
			CONFIGURATIONS Release RelWithDebInfo 
			PERMISSIONS
          	OWNER_READ OWNER_WRITE OWNER_EXECUTE 
				GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 
			COMPONENT Runtime
		)
      	#======plugins=====
		install(CODE "MESSAGE(\"install ${PKG_NAME} plugin dlls...\")")
      	file(GLOB PLUGINLIST ${CONAN_OSGEARTH_ROOT}/bin/osgPlugins*)
		#message(FATAL_ERROR "${CONAN_OSGEARTH_ROOT}/bin ---${PLUGINLIST}")
		foreach(FILE ${PLUGINLIST})
			if(IS_DIRECTORY ${FILE})
				get_filename_component(PLUGINDIRNAME ${FILE} NAME)
				file(GLOB ALL_PLUGIN_DLLS ${FILE}/*.dll)
			endif(IS_DIRECTORY ${FILE})
		endforeach(FILE)
		#message(FATAL_ERROR ${ALL_PLUGIN_DLLS})
		COMMON_GET_RELEASE_RUNTIME_FILES(ALL_PLUGIN_DLLS REL_PLUGIN_DLLS)
		#message(FATAL_ERROR ${REL_PLUGIN_DLLS})
      	install(FILES ${REL_PLUGIN_DLLS} 
			CONFIGURATIONS Release RelWithDebInfo 
			DESTINATION bin/${PLUGINDIRNAME} 
			PERMISSIONS
          	OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 
			COMPONENT Runtime
		)
   	else(WIN32)
		install(CODE "MESSAGE(\"install ${PKG_NAME} dlls...\")")
		file(GLOB REL_SOS1 ${CONAN_OSGEARTH_ROOT}/lib/*.so)
      	file(GLOB REL_SOS2 ${CONAN_OSGEARTH_ROOT}/lib/*.so*)
      	install(FILES ${REL_SOS1} ${REL_SOS2} 
			DESTINATION bin 
			PERMISSIONS
          	OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 
				COMPONENT Runtime
		)

		install(CODE "MESSAGE(\"install ${PKG_NAME} plugin dlls...\")")
      	file(GLOB PLUGINLIST ${CONAN_OSGEARTH_ROOT}/lib/osgPlugins*)
      	foreach(FILE ${PLUGINLIST})
			if(IS_DIRECTORY ${FILE})
				get_filename_component(PLUGINDIRNAME ${FILE} NAME)
				file(GLOB REL_PLUGIN_DLLS1 ${FILE}/*.so)
				file(GLOB REL_PLUGIN_DLLS2 ${FILE}/*.so*)
			endif(IS_DIRECTORY ${FILE})
      	endforeach(FILE)
		
      	install(FILES ${REL_PLUGIN_DLLS1} ${REL_PLUGIN_DLLS2} 
			DESTINATION bin/${PLUGINDIRNAME} 
			PERMISSIONS
          	OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 
			COMPONENT Runtime
		)
   endif(WIN32)
ELSE()
    message(NOTICE "${SUPPORT_3RD_PKG_NAME} Use Normal Package Mode....")
	#这里通过手动适配位置 by luli
	IF(NOT osgEarth_ROOT_DIR OR NOT EXISTS ${osgEarth_ROOT_DIR})
        #SET(osgEarth_ROOT_DIR ${PROJECT_3RDPARTY_DIR}/osgEarth-2.10.1-x64-vs2017)
		IF(WIN32)
			SEARCH_3RD_MAYBE_PATH(${SUPPORT_3RD_PKG_NAME}_ROOT_DIR 
				${PROJECT_3RDPARTY_DIR}/osgEarth-2.10.1-x64-vs2017
				${PROJECT_3RDPARTY_DIR}/osgEarth-2.10.1
			)
		ELSE()
			GET_THIRD_PARTY_PACKAGE_DIR_NAME("osgearth-2.10" full_package_name0)
			SEARCH_3RD_MAYBE_PATH(${SUPPORT_3RD_PKG_NAME}_ROOT_DIR 
				${PROJECT_3RDPARTY_DIR}/${full_package_name0}
			)
		ENDIF()
    ENDIF()

	#这里通过手动适配位置 by luli
	IF(NOT osg_ROOT_DIR OR NOT EXISTS ${osg_ROOT_DIR})
        IF(WIN32)
			SEARCH_3RD_MAYBE_PATH(${SUPPORT_3RD_PKG_NAME}_ROOT_DIR 
				${PROJECT_3RDPARTY_DIR}/osg-3.6.3-x64-vs2017
				${PROJECT_3RDPARTY_DIR}/osg-3.6.3
			)
		ELSE()
			SEARCH_3RD_MAYBE_PATH(${SUPPORT_3RD_PKG_NAME}_ROOT_DIR 
				${PROJECT_3RDPARTY_DIR}/osg-3.6.3-armv8
			)
		ENDIF()
    ENDIF()
	
	find_package(osgearth REQUIRED)
    set(osgearth_LIBRARY
        osgearth::osgEarth
        osgearth::osgEarthUtil 
        osgearth::osgEarthFeatures
        osgearth::osgEarthSymbology
    )
    set(osgEarth_LIBRARY
        ${osgearth_LIBRARY}
    )



    #调试环境变量
    MAKE_3RD_PKG_RUNTIME(${SUPPORT_3RD_PKG_NAME} ${osgEarth_ROOT_DIR})
    
    #打包使用
    if(WIN32)
		#========bin======
		install(CODE "MESSAGE(\"install ${PKG_NAME} dlls...\")")
    	file(GLOB ALL_DLLS ${osgEarth_ROOT_DIR}/bin/*.dll)
		COMMON_GET_RELEASE_RUNTIME_FILES(ALL_DLLS REL_DLLS)
      	install(FILES ${REL_DLLS} 
			DESTINATION bin 
			CONFIGURATIONS Release RelWithDebInfo 
			PERMISSIONS
          	OWNER_READ OWNER_WRITE OWNER_EXECUTE 
				GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 
			COMPONENT Runtime
		)
      	#======plugins=====
		install(CODE "MESSAGE(\"install ${PKG_NAME} plugin dlls...\")")
      	file(GLOB PLUGINLIST ${osgEarth_ROOT_DIR}/bin/osgPlugins*)
		#message(FATAL_ERROR "${osgEarth_ROOT_DIR}/bin ---${PLUGINLIST}")
		foreach(FILE ${PLUGINLIST})
			if(IS_DIRECTORY ${FILE})
				get_filename_component(PLUGINDIRNAME ${FILE} NAME)
				file(GLOB ALL_PLUGIN_DLLS ${FILE}/*.dll)
			endif(IS_DIRECTORY ${FILE})
		endforeach(FILE)
		#message(FATAL_ERROR ${ALL_PLUGIN_DLLS})
		COMMON_GET_RELEASE_RUNTIME_FILES(ALL_PLUGIN_DLLS REL_PLUGIN_DLLS)
		#message(FATAL_ERROR ${REL_PLUGIN_DLLS})
      	install(FILES ${REL_PLUGIN_DLLS} 
			CONFIGURATIONS Release RelWithDebInfo 
			DESTINATION bin/${PLUGINDIRNAME} 
			PERMISSIONS
          	OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 
			COMPONENT Runtime
		)
   	else(WIN32)
		install(CODE "MESSAGE(\"install ${PKG_NAME} dlls...\")")
      	file(GLOB REL_SOS ${osgEarth_ROOT_DIR}/lib64/*.so*)
      	install(FILES ${REL_SOS} 
			DESTINATION bin 
			PERMISSIONS
          	OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 
				COMPONENT Runtime
		)

		install(CODE "MESSAGE(\"install ${PKG_NAME} plugin dlls...\")")
      	file(GLOB PLUGINLIST ${osgEarth_ROOT_DIR}/lib64/osgPlugins*)
      	foreach(FILE ${PLUGINLIST})
			if(IS_DIRECTORY ${FILE})
				get_filename_component(PLUGINDIRNAME ${FILE} NAME)
				file(GLOB REL_PLUGIN_DLLS ${FILE}/*.so*)
			endif(IS_DIRECTORY ${FILE})
      	endforeach(FILE)

      	install(FILES ${REL_PLUGIN_DLLS} 
			DESTINATION bin/${PLUGINDIRNAME} 
			PERMISSIONS
          	OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 
			COMPONENT Runtime
		)
   endif(WIN32)
ENDIF()